package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.SchoolMapper;
import com.project.entity.School;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/5/4.
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class SchoolServiceImpl extends BaseService implements SchoolService {
    private static SchoolMapper schoolMapper=null;
    @Autowired
    private SchoolMangerService schoolMangerService;

    public void setMapper()
    {
        if(schoolMapper==null)
            schoolMapper = sqlSessionTemplate.getMapper(SchoolMapper.class);//1.获取相关mapper
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 增加学院
     * @param school
     * @return
     */
    @Override
    public int add(School school) {
        setMapper();
        return schoolMapper.insertSelective(school);
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id删除学院
     * @param id
     * @return
     */
    @Override
    public int remove(Integer id) {
        setMapper();
        return schoolMapper.deleteByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id修改学院信息
     * @param school
     * @return
     */
    @Override
    public int modify(School school) {
        setMapper();
        return schoolMapper.updateByPrimaryKeySelective(school);

    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id寻找学院信息
     * @param id
     * @return
     */
    @Override
    public School findOne(Integer id) {
        setMapper();
        return schoolMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据universityid寻找学院信息以及学院管理员
     *
     * @param universityid
     * @return
     */
    @Override
    public List<JSONObject> findBatchWithAdminByUniversityId(Integer universityid) throws Exception {
        setMapper();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<School> schoolList =  schoolMapper.selectByUniversityId(universityid);
        for (School school: schoolList){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("school",school);
            jsonObject.put("schoolManager",schoolMangerService.findOneBySchoolId(school.getId()));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据universityid寻找学院信息
     *
     * @param universityid
     * @return
     */
    @Override
    public List<School> findBatchByUniversityId(Integer universityid) {
        setMapper();
        return schoolMapper.selectByUniversityId(universityid);
    }


    /**
     * author: WheelChen
     * date: 2017/7/11
     * function: 检查是否已存在同一所学校相同学院
     *
     * @param school
     * @return true-存在 false-不存在
     */
    @Override
    public boolean checkUniversityIdAndNameIsExist(School school) {
        setMapper();
        return schoolMapper.checkUniversityIdAndNameIsExist(school) > 0;
    }


}
